﻿using System.Collections.Generic;
using System.Xml.Linq;

namespace xycn.pluginsupport.AspNetCorePlugins
{
    public class MidwareConfigure
    {
        private static XElement midwareConfiguration = null;

        //程序运行环境
        public static string RuntimeDir = "";

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="configuration"></param>
        public MidwareConfigure(XElement configuration)
        {
            if (midwareConfiguration == null)
            {
                midwareConfiguration = configuration;
            }
        }

        /// <summary>
        /// 获取指定Controller库的配置项
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static XElement GetConfig(string name)
        {
            XElement ret = null;

            if (midwareConfiguration == null)
                return null;

            if (name == null)
                return midwareConfiguration;

            //获取controller插件位置
            IEnumerable<XElement> midwares =
                midwareConfiguration.Elements();

            //遍历控制器插件配置
            foreach (XElement item in midwares)
            {
                XAttribute midware_attr = item.Attribute("name");
                if (midware_attr == null)
                {
                    continue;
                }
                if (midware_attr.Value == name)
                {
                    ret = item;
                    break;
                }
            }

            return ret;
        }
    }
}
